package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.OrderMapper;
import com.suda.pojo.Order;
import com.suda.service.OrderService;
import com.suda.service.WeixinPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/****
 * @Author:zhuyongjie
 * @Description:Order业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private WeixinPayService weixinPayService;

    @Autowired
    private OrderMapper orderMapper;


    /**
     * Order条件+分页查询
     * @param order 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Order> findPage(Order order, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(order);
        //执行搜索
        return new PageInfo<Order>(orderMapper.selectByExample(example));
    }

    private Set<String>set=new HashSet<>();
    /**
     * Order分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Order> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        List<Order> orders = orderMapper.selectAll();
        for (Order order : orders) {
            if(set.contains(order.getOrderId())) continue;
            Map<String,String> map = weixinPayService.queryPayStatus(order.getOrderId());
            String return_code = map.get("return_code");
            //业务结果
            String result_code = map.get("result_code");
            if(result_code==null||return_code==null) continue;
            //业务结果 result_code=SUCCESS/FAIL，修改订单状态
            if(return_code.equalsIgnoreCase("success")&&result_code.equalsIgnoreCase("success") ){
                //获取订单号
                //业务结果
                order.setPayStatus("payment_received");
                set.add(order.getOrderId());
                update(order);
            }
        }

        //分页查询
        return new PageInfo<Order>(orderMapper.selectAll());
    }

    /**
     * Order条件查询
     * @param order
     * @return
     */
    @Override
    public List<Order> findList(Order order){
        //构建查询条件
        Example example = createExample(order);
        //根据构建的条件查询数据
        return orderMapper.selectByExample(example);
    }


    /**
     * Order构建查询对象
     * @param order
     * @return
     */
    public Example createExample(Order order){
        Example example=new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if(order!=null){
            // 订单id
            if(!StringUtils.isEmpty(order.getOrderId())){
                    criteria.andEqualTo("orderId",order.getOrderId());
            }
            // 包裹id
            if(!StringUtils.isEmpty(order.getPackageId())){
                    criteria.andEqualTo("packageId",order.getPackageId());
            }
            // 收件时间
            if(!StringUtils.isEmpty(order.getReceiveTime())){
                    criteria.andEqualTo("receiveTime",order.getReceiveTime());
            }
            // 收件员id
            if(!StringUtils.isEmpty(order.getReceiveCourierId())){
                    criteria.andEqualTo("receiveCourierId",order.getReceiveCourierId());
            }
            // 寄件客户id
            if(!StringUtils.isEmpty(order.getConsignorId())){
                    criteria.andEqualTo("consignorId",order.getConsignorId());
            }
            // 收件客户id
            if(!StringUtils.isEmpty(order.getConsigneeId())){
                    criteria.andEqualTo("consigneeId",order.getConsigneeId());
            }
            // 派件员id
            if(!StringUtils.isEmpty(order.getDistributerId())){
                    criteria.andEqualTo("distributerId",order.getDistributerId());
            }
            // 预计派件时间
            if(!StringUtils.isEmpty(order.getEstiDistriTime())){
                    criteria.andEqualTo("estiDistriTime",order.getEstiDistriTime());
            }
            // 实际派件时间
            if(!StringUtils.isEmpty(order.getActualDistriTime())){
                    criteria.andEqualTo("actualDistriTime",order.getActualDistriTime());
            }
            // 支付类型
            if(!StringUtils.isEmpty(order.getPayType())){
                    criteria.andEqualTo("payType",order.getPayType());
            }
            // 费用
            if(!StringUtils.isEmpty(order.getFee())){
                    criteria.andEqualTo("fee",order.getFee());
            }
            // 支付状态
            if(!StringUtils.isEmpty(order.getPayStatus())){
                    criteria.andEqualTo("payStatus",order.getPayStatus());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        orderMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Order
     * @param order
     */
    @Override
    public void update(Order order){
        orderMapper.updateByPrimaryKey(order);
    }

    /**
     * 增加Order
     * @param order
     */
    @Override
    public void add(Order order){
        orderMapper.insert(order);
    }

    /**
     * 根据ID查询Order
     * @param id
     * @return
     */
    @Override
    public Order findById(String id){
        return  orderMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Order全部数据
     * @return
     */
    @Override
    public List<Order> findAll() {
        return orderMapper.selectAll();
    }
}
